package com.webcamtracker.tracker.model;

import com.webcamtracker.configuration.RootPref;
import com.webcamtracker.image.Filter;
import com.webcamtracker.tracker.TrackingContext;

import java.util.Arrays;
import java.util.List;


public class ModelElementComparator {

    private List<Filter> filters;


    public ModelElementComparator(Filter... filters) {
        this.filters = Arrays.asList(filters);
    }

    public boolean accept(ModelElement current, TrackingContext context) {
        int hits = 0;


        double x2 = current.getX();
        double y2 = current.getY();
        double end2X = current.getMaxX();
        double end2Y = current.getMaxY();
        int acceptance = RootPref.getGridPref().getInt(RootPref.KEY_GRID_ACCEPTANCE, RootPref.DEF_VALUE_GRID_ACCEPTANCE);
        while (x2 < end2X) {
            while (y2 < end2Y) {

                boolean similar = true;

                for (Filter filter : filters) {
                    similar = filter.accept(current, x2, y2, context);

                    if (!similar) {
                        break;
                    }
                }

                if (similar) {
                    hits++;
                    if (hits >= acceptance) {
                        return true;
                    }
                }

                y2++;

            }
            x2++;

        }
        return false;
    }
}
